home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / AXUI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-13  |  6.5 KB  |  229 lines

  1. /*
  2.  * axui <iface> [unproto_call]   --   send unproto packets via <iface>.
  3.  * From TNOS by KO4KS.  Mods by K5JB.
  4.  */
  5. #include "global.h"
  6. #ifdef AXUI
  7. #include "ctype.h"
  8. #include "commands.h"
  9. #include "session.h"
  10. #include "smtp.h"
  11. #include "usock.h"
  12. #include "mailbox.h"
  13. #include "pktdrvr.h"
  14.  
  15. #if !defined(_lint)
  16. static char rcsid[] OPTIONAL = "$Id: axui.c,v 1.16 1997/06/13 13:15:31 root Exp root $";
  17. #endif
  18.  
  19. int Axui_sock = -1;        /* Socket number listening for AX25 UI frames */
  20. static struct iface *axif;
  21. static int ui_timestamp = 1;    /* Time and etc, in displayed header - K5JB */
  22. static int ui_verbose = 0;    /* Toggle on/off verbose headers */
  23. static int ui_header = 1;    /* Toggle on/off use of headers */
  24. static int ui_silent = 0;    /* Toggle on/off supression of bell characters */
  25.  
  26. static char lastsrc[AXALEN], lastdest[AXALEN];
  27.  
  28. static char help[] = "<Cmds>:\n  /c call (set new outgoing call)      /i iface (set new outgoing interface)\n  /h      (toggle use of headers)      /v       (verbose headers - use always)\n  /t      (toggle use of timestamp)    /q       (to quit)\n";
  29. static char running[] = "%s already running\n";
  30. static char notax25[] = "Iface %s not an AX25 type interface\n";
  31. static char notdefined[] = "Iface %s not defined\n";
  32. static char sessionstr[] = "%s%s session %d UI frames %s->%s on interface %s\n%s";
  33. static char forhelp[] = "For help, type '/?'\n";
  34. static char badiface[] = "<invalid interface>\n";
  35. static char headersstr[] = "*** Headers %sabled\n";
  36. static char Enstr[] = "En";
  37. static char Disstr[] = "Dis";
  38. static char verbosestr[] = "*** Headers displayed %s\n";
  39. static char packetstr[] = "for each packet";
  40. static char changestr[] = "when src or dest changes";
  41. static char timestamp[] = "*** Timestamping in headers is %sabled\n";
  42. static char silentstr[] = "*** Silent mode is %sabled\n";
  43. static char sessionclosed[] = "\n%s session %u closed: EOF\n";
  44. static char display1[] = "\n%s - %s recv: %s->%s:\n";
  45. static char display2[] = "\n%s->%s:\n";
  46. static char DigisUsed[] = "Illegal when digis in use.\n";
  47.  
  48.  
  49. int 
  50. doaxui (int argc, char *argv[], void *p OPTIONAL)
  51. {
  52. char *cp;
  53. char name[AXBUF];
  54. char buf[200];
  55. int16 i;
  56. char tmpcall[AXALEN];
  57. char tmpcall2[AXBUF];
  58. struct session *sp;
  59. struct mbuf *bp;
  60. struct iface *ifc;
  61. int first = 1;
  62.  
  63.     /* Check if this comes from console */
  64.     if (Curproc->input != Command->input)
  65.         return 0;
  66.  
  67.     /* Check to see if AXUI is already running. Only one copy at a time */
  68.     if (Axui_sock != -1) {
  69.         tprintf (running, Sestypes[AXUITNC]);
  70.         return 1;
  71.     }
  72.     if (((axif = if_lookup (argv[1])) != NULLIF) && (axif->type != CL_AX25)) {
  73.         tprintf (notax25, argv[1]);
  74.         return 1;
  75.     }
  76.     if (axif == NULLIF) {
  77.         tprintf (notdefined, argv[1]);
  78.         return 1;
  79.     }
  80.     if (argc == 2 || setcall (tmpcall, argv[2]) == -1)
  81.         memcpy (tmpcall, Ax25multi[IDCALL], AXALEN);
  82.  
  83.     if (argc > 3)        /* digis present? */
  84.         if (connect_filt (argc, argv, tmpcall, axif, AX_AUTO) == 0)
  85.             return 1;
  86.  
  87.     (void) pax25 (name, AXuser);
  88.     /* Now everything seems okay ! Get a session */
  89.     if ((sp = newsession (name, AXUITNC, 1)) == NULLSESSION) {
  90.         tputs (TooManySessions);
  91.         return 0;
  92.     }
  93.  
  94. restart:
  95.     tprintf (sessionstr, (first) ? "" : "\n", Sestypes[sp->type], sp->index, pax25 (tmpcall2, AXuser),
  96.          pax25 (name, tmpcall), axif->name, (first) ? forhelp : "");
  97.     Axui_sock = Curproc->output;
  98.     first = 0;
  99.  
  100.     /* Process whatever's typed on the terminal */
  101.     memset (buf, 0, MBXLINE);    /* Clear the input buffer */
  102.     while (recvline (Curproc->input, (unsigned char *) buf, sizeof (buf) - 1) >= 0) {
  103.         if (buf[0] == '/') {
  104.             cp = skipnonwhite (buf);
  105.             cp = skipwhite (cp);
  106.             rip (cp);
  107.             /* process commands */
  108.             switch (tolower (buf[1])) {
  109.                 case '?':
  110.                     tputs (help);
  111.                     goto restart;
  112.                 case 'c':
  113.                     if (argc > 3) {
  114.                         tputs (DigisUsed);
  115.                         break;
  116.                     }
  117.                     if (setcall (tmpcall2, cp) == -1)
  118.                         break;
  119.                     memcpy (tmpcall, tmpcall2, AXALEN);
  120.                     goto restart;
  121.                 case 'i':
  122.                     if (argc > 3) {
  123.                         tputs (DigisUsed);
  124.                         break;
  125.                     }
  126.                     if (((ifc = if_lookup (cp)) != NULLIF) && (ifc->type == CL_AX25)) {
  127.                         axif = ifc;
  128.                         goto restart;
  129.                     } else
  130.                         tputs (badiface);
  131.                     break;
  132.                 case 'h':
  133.                     ui_header ^= 1;
  134.                     tprintf (headersstr, (ui_header) ? Enstr : Disstr);
  135.                     break;
  136.                 case 'v':
  137.                     ui_verbose ^= 1;
  138.                     tprintf (verbosestr, (ui_verbose) ? packetstr : changestr);
  139.                     break;
  140.                 case 't':
  141.                     ui_timestamp ^= 1;
  142.                     tprintf (timestamp, (ui_timestamp) ? Enstr : Disstr);
  143.                     break;
  144.                 case 's':
  145.                     ui_silent ^= 1;
  146.                     tprintf (silentstr, (ui_silent) ? Enstr : Disstr);
  147.                     break;
  148.                 case 'b':
  149.                 case 'e':
  150.                 case 'q':
  151.                     goto done;
  152.                 default:
  153.                     tputs ("Huh?\n");
  154.                     break;
  155.             }
  156.         } else {
  157.             i = (int16) strlen (buf);
  158.             buf[i - 1] = '\r';
  159.             if ((bp = alloc_mbuf (i)) == NULLBUF)
  160.                 goto done;
  161.             bp->cnt = i;
  162.             memcpy (bp->data, buf, (size_t) i);
  163.  
  164.             (void) (*axif->output) (axif, tmpcall, AXuser, PID_NO_L3, bp);    /* send it */
  165.         }
  166.         usflush (Curproc->output);
  167.         memset (buf, 0, MBXLINE);    /* Clear the input buffer */
  168.     }
  169. done:
  170.     if (argc > 3)
  171.         (void) ax_drop (tmpcall, axif, AX_AUTO);    /* remove digi route added by connect_filt */
  172.     Axui_sock = -1;
  173.     tprintf (sessionclosed, Sestypes[sp->type], sp->index);
  174.     (void) keywait (NULLCHAR, 1);
  175.     freesession (sp);
  176.     return 0;
  177. }
  178.  
  179.  
  180. void
  181. axui_input (iface, axp, src, dest, bp, mcast)
  182. struct iface *iface;
  183. struct ax25_cb *axp OPTIONAL;
  184. char *src;
  185. char *dest;
  186. struct mbuf *bp;
  187. int mcast OPTIONAL;
  188. {
  189. time_t timer;
  190. char *cp;
  191. char buf[256];
  192. int16 n, nn = 0;
  193. char thissrc[AXBUF], thisdest[AXBUF];
  194.  
  195.     if (Axui_sock != -1) {
  196.         (void) pax25 (thissrc, src);
  197.         (void) pax25 (thisdest, dest);
  198.         if (ui_header && (ui_verbose || memcmp (lastsrc, src, AXALEN) || memcmp (lastdest, dest, AXALEN))) {
  199.             memcpy (lastsrc, src, AXALEN);
  200.             memcpy (lastdest, dest, AXALEN);
  201.             if (ui_timestamp) {
  202.                 (void) time (&timer);
  203.                 cp = ctime (&timer);
  204.                 cp[24] = '\0';
  205.                 usprintf (Axui_sock, display1, cp, iface->name, thissrc, thisdest);
  206.             } else
  207.                 usprintf (Axui_sock, display2, thissrc, thisdest);
  208.         }
  209.         while ((n = pullup (&bp, (unsigned char *) buf, sizeof (buf) - 1)) != 0) {
  210.             buf[n] = 0;
  211.             if (ui_silent) {
  212.                 do {
  213.                     cp = strchr (buf, 0x07);
  214.                     if (cp)
  215.                         *cp = '.';
  216.                 } while (cp);
  217.             }
  218.             nn = (int16) usprintf (Axui_sock, "%s", buf);
  219.         }
  220.         if (nn && buf[nn - 1] != '\n')
  221.             usputc (Axui_sock, '\n');
  222.         usflush (Axui_sock);
  223.     }
  224.     free_p (bp);
  225. }
  226.  
  227.  
  228. #endif /* AXUI */
  229.